<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Buyer\Controller;
use Think\Controller;
use User\Api\UserApi;
use Think\Model;

class ReportController extends Controller {
    public function myreport(){
        $this->title="报价商品";
        $res1 = M('Purchase_list')->where(array("status"=>2))->find();//查询状态为2的代采购的清单。历史为2的已删除
        $batch = $res1['batch'];
        $goods_map['batch'] = $batch;
        $goods_map['status'] = array('lt',2);
        $have_read = M('Purchase_list')->where($goods_map)->getField('have_read');//获取用户是否看过报价
        $readarr = explode(",",$have_read);
        //判断用户之前是否看过
        if(in_array(session('userid'),$readarr)){
        }else{
            $have_read.="".session('userid').",";
            $read_data['have_read'] = $have_read;
            M('Purchase_list')->where($goods_map)->setField('have_read',$have_read);//记录用户查看过该报价
        }
        $pidarr = [];
        $goodlist = M('Purchase')->where($goods_map)->select();
        foreach($goodlist as $key => $value){
            array_push($pidarr,$value['pid']);//获取当前待采购的所有pid
        }
        $perarr = get_provider_permission();
        $inter = array_intersect($perarr,$pidarr);
        $list = [];
        foreach($goodlist as $key => $value){
            //商品权限
            if(in_array($value['pid'],$inter)){
                array_push($list,$goodlist[$key]);
            }
        }
        foreach($list as $key => $value){
            $report_map['batch'] = $value['batch'];
            $report_map['pid'] = $value['pid'];
            $report_map['uid'] = session("userid");
            $report_info = M("Provider_report")->where($report_map)->Order("createdAt desc")->limit(1)->select();
            if($report_info){
                $list[$key]['reported'] = 1;
                $list[$key]['oldprice'] = $report_info[0]['price'];
                $list[$key]['oldmark'] = $report_info[0]['mark'];
                $list[$key]['oldcreatedAt'] = $report_info[0]['createdAt'];
            }else{
                $list[$key]['reported'] = 0;
            }
        }
        $this->assign("_goods",$list);
        $this->display();
    }
    public function givereport(){
        $batch = I("batch");
        $uid = session("userid");
        $pid = I("pid");
        $search['batch'] = $batch;
        $search['pid'] = $pid;
        $res2 = M("Purchase")->where($search)->find();
        if($res2['status']>1){
            echo 2;
            return false;
        }
        $map['batch'] = $batch;
        $map['pid'] = $pid;
        $map['uid'] = $uid;
        $map['status'] = 1;
        $res = M("Provider_report")->where($map)->select();
        foreach($res as $key => $val){
            M('Provider_report')->where($val)->save(array('status'=>0));
        }
        $data['batch'] = I("batch");
        $data['mark'] = I("mark");
        $data['price'] = I("price");
        $data['pid'] = I("pid");
        $data['uid'] = session("userid");
        $data['createdAt'] = time();
        $data['updatedAt'] = time();
        $data['status'] = 1;
        if(M("Provider_report")->add($data)){
            echo 1;
        }else{
            echo 0;
        }
    }

}
